/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.aggjpsv.libraryserver.error;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author SergioPeres
 */
public class Error {
    
    protected static final DateFormat HTTP_DATE_FORMAT = new SimpleDateFormat(
            "EEE, dd MMM yyyy HH:mm:ss z");
    
    private static String getHead(String codErro,String tipoRequisicao) {
        // Head
        StringBuilder sb = new StringBuilder();
        // Cria primeira linha do status code, no caso 200 OK
        sb.append("HTTP/1.1 " + codErro).append("\r\n");

        // Cria os cabeçalhos
        sb.append("Date: ").append(HTTP_DATE_FORMAT.format(new Date()))
                .append("\r\n");
        sb.append("Server: Web Server")
                .append("\r\n");
        sb.append("Connection: Close").append("\r\n");
        sb.append("Content-Type: " + tipoRequisicao).append("\r\n");
        sb.append("\r\n");
        return sb.toString();
    }
    
    public static String returnHTMLError(int errorCode, String req, int port)
    {
        String title, h1, messengerError;
        title = h1 = messengerError = "";
        String res = "";
        
        switch (errorCode)
        {
            case 404:
                res = getHead("404 Not found","text/html; charset=UTF-8");
                title = "404 Not Found";
                h1 = "Not Found";
                messengerError = "        <p>The requested URL " + req + " was not found on this server.</p>\n"
                        + "        <p>\n"
                        + "            Additionally, a 404 Not Found\n"
                        + "            terror was encountered while trying to use an ErrorDocument to handle the request.\n"
                        + "        </p>\n";
                break;
                
            case 400:
                res = getHead("400 Bad request","text/html; charset=UTF-8");
                title = "400 Bad Request";
                h1 = "Bad Request";
                break;
                
            default:
                res = getHead("500 Internal Server Error","text/html; charset=UTF-8");
                title = "500 Internal Server Error";
                h1 = "Internal Server Error";
                messengerError = "        <br /><br />\n"
                        + "        <h3>\n"
                        + "            <marquee>Sorry! Server error...  :'(</marquee>\n"
                        + "        </h3>\n"
                        + "        <br /><br />\n";
                break;
        }
        
        res += "<!DOCTYPE html>\n"
                + "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
                + "    <head>\n"
                + "        <title>" + title + "</title>\n"
                + "    </head>\n"
                + "    <body>\n"
                + "        <h1>" + h1 + "</h1>\n"
                + "        <!-- Messenger error! -->\n\n"
                + messengerError
                + "\n\n        <!-- Fim messenger error -->\n"
                + "        <hr />\n"
                + "        <address>Web Server at " + "" + " Port " + port + "</address>\n"
                + "    </body>\n"
                + "</html>";
        return res;
    }
}
